package com.ruoyi.business.tool;

import com.ruoyi.business.BO.MatchContractInfoBO;
import com.ruoyi.business.mapper.AcContractInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Random;

@Service
public class ContractInfoTool {

    @Autowired
    private AcContractInfoMapper acContractInfoMapper;

    /**
     * 随机生成租赁编号
     * 格式：yyyyMMddXXX
     * **/
    public String generateRandomNumber(MatchContractInfoBO matchContractInfoBO) {
        // 获取当前日期
        LocalDate currentDate = LocalDate.now();
        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        // 格式化当前日期为 yyyyMMdd 格式
        String result = currentDate.format(formatter);
        Random random = new Random();
        while (true) {
            // 生成一个三位数的随机数，范围从 0 到 999
            int randomThreeDigitNumber = random.nextInt(1000);
            // 使用 String.format 确保数字始终为三位数，包括前导零
            String formattedNumber = String.format("%03d", randomThreeDigitNumber);
            matchContractInfoBO.setContractCode(result+formattedNumber);
            matchContractInfoBO.setDelFlag("0");
            matchContractInfoBO.setContractIsDelete(0);
            int count = acContractInfoMapper.matchContractCode(matchContractInfoBO);
            if(0 == count) {
                result = result + formattedNumber;
                break;
            }
        }
        return result;
    }
}
